06. Choosing Initial Gains
Choosing Initial Gains
Now that you've coded up your controller, it's time to pick some initial gains and see how it works. For the course, we knew the exact properties of our drone in the simulator, allowing us to calculate some initial gains mathematically, but here, that's not so much the case. So let's see if we can build some intuition and do some back of the envelope calculation to decide on some gains.
First things first, let's make sure we don't ever try and go too fast and add a limit on the allowable velocity command. Let's keep things nice and slow and limit the velocity to 0.3 m/s.
As a recap, our controller is calculating the position error (how far we are from where we'd like to be) and multiplying that by our gain (self._kp_pos
and self._kp_alt
) to generate a velocity command. In general, when we are far away from our target position (pos_error >> 0), we'd like to approach it at our max velocity, so let's not worry about that case since we've added a mechanism to limit our velocity command to a max velocity. What really starts to come in to play here is at what distance do you want to start slowing down. The easiest way to start making these decisions is looking at examples, so let's make up a few.
For example, let's set our max velocity to 1 m/s and set _kp_pos = 0.5
, let's take a look at what it means for our Crazyflie's velocity profile as it approaches the waypoint:
- at 1 meter away, we will be flying 0.5 m/s
- at 0.5 meters away, we will be flying at 0.25 m/s
Now let's say we set _kp_pos = 10
, let's take a look at what it means:
- at 1 meter away, we will be flying at 10 m/s (or max velocity)
- at 0.5 meters away, we will be flying at 5 m/s (or max velocity)
For those who need a bit more of a visualization, go ahead and plot it out, you will see that as you increase your gain, the slope of the resulting velocity can get impossibly steep!
Do you think that the Crazyflie will be able to stop without overshooting if it's still flying at 5 m/s when it's 0.5 meters away from the target position?
If the Crazyflie could respond immediately to commands and instantaneously change its velocity vector, then yes, this would be possible, but alas, in the real world, we have delays, it takes some time to change velocity (and attitude), which results in needing to tune our controller. Different drones will be able to react at different rates, allowing for much more aggressive control on some drones versus others.
Do you think you could approach the target position faster than 0.25 m/s when 0.5 meters out?
These are the questions that you should be asking yourself as you choose an initial set of gains to work with for your controller. You should err on the side of caution and go with the option that gives you room to increase your gain to get better performance rather than have to decrease your gain.
As a last part to build some of our intuition, we can see that setting the max speed value allows us to have very aggressive control without the proportional, potentially impossibly high, speed increase when pos_error >> 0.
In the next controller (the inner loop controller), this same thought process can also be used to get an idea for what kind of gain might seem like a reasonable starting point. It gets a little trickier as the units get a bit less intuitive (attitude will all be in radians), but the same intuition can give you a helping hand!
It is worth mentioning that in this case there really isn't such a thing as too small of a value. Worse case your Crazyflie will just not go anywhere, but it also won't fall out of the sky. When it comes to lower level control loops, for example an attitude controller that is responsible for keeping your drone level, you will start seeing cases where you can have a gain that is too low and results in your drone falling out of the sky!
Additional resources for PID tuning: